<script setup>
import { onMounted, onUnmounted } from "vue";
import AMapLoader from "@amap/amap-jsapi-loader";

let map = null;
window._AMapSecurityConfig = {
  securityJsCode: "c9529f86b1cac0d22675a4d1755935d6"
};
onMounted(() => {
  AMapLoader.load({
    key: "	f174a4b436c7b68c9e16e242a92ef409", // 申请好的Web端开发者Key，首次调用 load 时必填
    version: "2.0", // 指定要加载的 JSAPI 的版本，缺省时默认为 1.4.15
    plugins: ["AMap.Scale", "AMap.ToolBar", "AMap.ControlBar", "AMap.HawkEye"] //需要使用的的插件列表，如比例尺'AMap.Scale'，支持添加多个如：['...','...']
  })
    .then(AMap => {
      map = new AMap.Map("container", {
        // 设置地图容器id
        viewMode: "3D", // 是否为3D地图模式
        zoom: 11, // 初始化地图级别
        center: [116.397428, 39.90923] // 初始化地图中心点位置
      });
      //地图加载
      var map = new AMap.Map("container", {
        resizeEnable: true
      });
      //输入提示
      var autoOptions = {
        input: "tipinput"
      };

      AMap.plugin(["AMap.PlaceSearch", "AMap.AutoComplete"], function() {
        var auto = new AMap.AutoComplete(autoOptions);
        var placeSearch = new AMap.PlaceSearch({
          map: map
        }); //构造地点查询类
        auto.on("select", select); //注册监听，当选中某条记录时会触发
        function select(e) {
          placeSearch.setCity(e.poi.adcode);
          placeSearch.search(e.poi.name); //关键字查询查询
        }
      });
      var scale = new AMap.Scale({
          visible: true
        }),
        toolBar = new AMap.ToolBar({
          visible: true,
          position: {
            top: "110px",
            right: "40px"
          }
        }),
        controlBar = new AMap.ControlBar({
          visible: true,
          position: {
            top: "10px",
            right: "10px"
          }
        }),
        overView = new AMap.HawkEye({
          visible: true
        });
      map.addControl(scale);
      map.addControl(toolBar);
      map.addControl(controlBar);
      map.addControl(overView);
    })
    .catch(e => {
      console.log(e);
    });
});

onUnmounted(() => {
  map?.destroy();
});
</script>

<template>
  <div class="conent">
    <div id="container"></div>
    <div class="tops">
      <table>
        <tr>
          <td>
            <label>请输入关键字：</label>
          </td>
        </tr>
        <tr>
          <td>
            <input id="tipinput" />
          </td>
        </tr>
      </table>
    </div>
  </div>
</template>

<style scoped>
.conent {
  width: 100%;
  height: 100%;
}
#container {
  width: 100%;
  height: 500px;
}
.tops {
  position: absolute;
  top: 20px;
}
</style>
